package org.jxht.service.impl;

import org.jxht.dto.entity.RequestScoreEntity;
import org.jxht.dto.entity.ResponseCourseEntity;
import org.jxht.dto.entity.ResponseScoreEntity;
import org.jxht.dao.StuCouDao;
import org.jxht.dto.StuCouDto;
import org.jxht.service.StuCouService;
import org.jxht.util.ConstantUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by huang on 2017/3/15 0015.
 */
@Service
public class StuCouServiceImpl implements StuCouService {

    @Resource
    StuCouDao stuCouDao;

    //    插入一条学生课程记录
    public String insertStuCouService(StuCouDto dto) {
        String creditCount = stuCouDao.querySelectCouCreditCount(Integer.toString(dto.getStuId()));
        int Count = 0; //记录学生在该学期的选修课程总学分
        if(creditCount != null){
            /*查询学生选修课程总学分*/
            Count = Integer.parseInt(creditCount)+stuCouDao.querySelectCouCreditSigle(Integer.toString(dto.getTeaCouId()));
        }
        if( Count >4){
            return "CREDIT_ERROR";//选课失败消息
        }
        //插入学生选择该课程记录
        stuCouDao.insertStuCouDao(dto);
        return "SUCCESS";//选课成功消息
    }

    public List queryStuCouService(StuCouDto dto) {
        return stuCouDao.queryStuCouDao(dto);
    }

    public StuCouDto queryStuCouServiceSingle(StuCouDto dto) {
        return (StuCouDto) stuCouDao.queryStuCouDao(dto).get(0);
    }

    public void updataStuCouService(StuCouDto dto) {
        stuCouDao.updataStuCouDao(dto);
    }

    public void deleteStuCouService(StuCouDto dto) {
        stuCouDao.deleteStuCouDao(dto);
    }

    public List queryselectCouRecord(ResponseCourseEntity entity) {
        return stuCouDao.queryselectCouRecord(entity);
    }

    //    获取课程成绩
    public List<ResponseScoreEntity> queryStuScore(int requestId, String requestWay) {
        RequestScoreEntity entity = new RequestScoreEntity();
        if (ConstantUtils.STU_ID.equals(requestWay)) {
            entity.setStuId(requestId);
        } else if (ConstantUtils.TEA_ID.equals(requestWay)) {
            entity.setTeaId(requestId);
        } else if (ConstantUtils.COU_ID.equals(requestWay)) {
            entity.setCouId(requestId);
        } else {
            entity.setTeaCouId(requestId);
        }
        return stuCouDao.queryStuCouScoreById(entity);
    }

    //    学生申请重修
    public int RenewalApplicationCourse(StuCouDto dto) {

        StuCouDto stuCouDto = queryStuCouServiceSingle(dto);
        stuCouDto.setCouRetake("重修");
        if(stuCouDto.getStuCouScore()<60 && ("".equals(stuCouDto.getCouRetake())|| null ==stuCouDto.getCouRetake())) {
            stuCouDao.updataStuCouDao(dto);
            return 1;
        }
        else if(stuCouDto.getStuCouScore()<70 && ("重修".equals(stuCouDto.getCouRetake()))){
            stuCouDao.updataStuCouDao(dto);
            return 1;
        }else {
            return 0;
        }
    }
}